home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / DATATYPE.SWG / 0009_TYPECST2.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  876b  |  26 lines

  1.  > Yes LongInts are as you say from approx -2bil to +2bil.  I'd
  2.  > say what is happening here is that you are adding two
  3.  > Integers & assigning the result to a LongInt.  Consider the
  4.  > following :-
  5.  
  6.  > Var
  7.  >    v1, v2 : Integer;
  8.  >    Res    : LongInt;
  9.  
  10.  > begin
  11.  >      v1 := 30000;
  12.  >      v2 := 30000;
  13.  >      Res := v1 + v2;
  14.  > end;
  15.  
  16.  > This will not give Res = 60000, because as Far as I am aware
  17.  > TP only does Type promotion to the RHE Until the actual
  18.  > assignment operation.  What this means is that the sum of v1
  19.  > & v1 must yield an Integer since the largest Type to contain
  20.  > each is an Integer.  Adding two Integer 30000 numbers
  21.  > together caUses an overflow & ends up being a random-ish
  22.  > number, usually negative.  So what must be done here is
  23.  > Typecasting.  This should fix it :-
  24.  
  25.  >      Res := LongInt(v1) + LongInt(v2);
  26.